package com.gexiaobao.pigeon.ui.fragment.setting

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.countDownCoroutines
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.EditMobileParams
import com.gexiaobao.pigeon.app.model.param.GetVerCodeParams
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentModifyPhoneBinding
import com.gexiaobao.pigeon.viewmodel.PersonalInfoViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 16:36
 * @Describe    :
 */
class FragmentModifyPhone : BaseFragment<PersonalInfoViewModel, FragmentModifyPhoneBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        mDatabind.includeBar.toolbar.initClose("修改手机号") {
            nav().navigateUp()
        }

        mViewModel.sendVerificationCode.set("发送验证码")
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvVerifyCodeSend, mDatabind.updatePhoneCommit) { it ->
            when (it) {
                mDatabind.tvVerifyCodeSend -> {
                    when {
                        mViewModel.sendVerificationCode.get()
                            .isEmpty() -> showMessage("请输入验证码")

                        else -> {
                            //开启倒计时60s
                            job = countDownCoroutines(60, {
                                mViewModel.sendVerificationCode.set("${it}s")
                            }, {
                                mViewModel.sendVerificationCode.set("获取验证码")
                            }, lifecycleScope)
                            val body = GetVerCodeParams(mViewModel.newPhone.get(), 5)
                            mViewModel.getVerCode(body)
                        }
                    }
                }

                mDatabind.updatePhoneCommit -> {
                    when {
                        mViewModel.newPhone.get().isEmpty() -> showMessage("请输入手机号")
                        !Util.isTelPhoneNumber(mViewModel.newPhone.get()) -> showMessage(
                            resources.getString(
                                R.string.user_phone_number_wrong
                            )
                        )

                        mViewModel.verCode.get().isEmpty() -> showMessage("请输入验证码")
                        mViewModel.verCode.get().length != 6 -> showMessage(resources.getString(R.string.user_verification_code_wrong))
                        else -> {
                            val body = EditMobileParams(
                                mobile = mViewModel.newPhone.get(), code = mViewModel.verCode.get()
                            )
                            mViewModel.editMobile(body)
                        }
                    }
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.editMobileResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }
    }
}